using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Pong
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Paddle : Actor
    {
        bool control;
        private KeyboardState keyboard;
        Game1 mainReference;


        public Paddle(Game1 game, bool control = true)
            : base(game)
        {
            this.control = control;
            mainReference = game;
            mainReference.Components.Add(this);
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if (control)
            {
                letterControl();
            }
            else
            {
                arrowControl();
            }

            base.Update(gameTime);
        }

        private void letterControl()
        {
            keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.W))
            {
                if (position.Y + 5 >= 0)
                {
                    this.position.Y -= 5;
                }
            }
            if (keyboard.IsKeyDown(Keys.S))
            {
                if (position.Y - 5 <= mainReference.screenHeight - this.source.Height)
                {
                    this.position.Y += 5;
                }
            }
        }

        private void arrowControl()
        {
            keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.Up))
            {
                if (position.Y + 5 >= 0)
                {
                    this.position.Y -= 5;
                }
            }
            if (keyboard.IsKeyDown(Keys.Down))
            {
                if (position.Y - 5 <= mainReference.screenHeight - this.source.Height)
                {
                    this.position.Y += 5;
                }
            }
        }
    }
}
